<?php
function fb_fan_count($facebook_name) {
// Example: https://graph.facebook.com/digimantra
    $data = json_decode(file_get_contents("https://graph.facebook.com/" . $facebook_name));
    echo $data->likes;
}

function get_current_weather() {
    $xml = simplexml_load_file('http://www.google.com/ig/api?weather=ADDRESS');
    $information = $xml->xpath("/xml_api_reply/weather/current_conditions/condition");
    echo $information[0]->attributes();
}

function getLatLong($address) {
    if (!is_string($address))
        die("All Addresses must be passed as a string");
    $_url = sprintf('http://maps.google.com/maps?output=js&q=%s', rawurlencode($address));
    $_result = false;
    if ($_result = file_get_contents($_url)) {
        if (strpos($_result, 'errortips') > 1 || strpos($_result, 'Did you mean:') !== false)
            return false;
        preg_match('!center:\s*{lat:\s*(-?\d+\.\d+),lng:\s*(-?\d+\.\d+)}!U', $_result, $_match);
        $_coords['lat'] = $_match[1];
        $_coords['long'] = $_match[2];
    }
    return $_coords;
}

//
function getTinyUrl($url) {
    return file_get_contents("http://tinyurl.com/api-create.php?url=" . $url);
}

function send_email($to_address, $to_name, $from_address, $from_name, $reply_address, $reply_name, $subject, $htmlbody, $plainbody, $host, $username, $password) {
    include("class.phpmailer.php");
	$mail = new PHPMailer();

	$mail->IsSMTP();                // set mailer to use SMTP
	$mail->Host = $host;  // specify main and backup server
	$mail->SMTPAuth = true;     // turn on SMTP authentication
	$mail->Username = $username;  // SMTP username
	$mail->Password = $password; // SMTP password

	$mail->From = $from_address;
	$mail->FromName = $from_name;
	$mail->AddAddress($to_address, $to_name);                 // name is optional
	$mail->AddReplyTo($reply_address, $reply_name);

	$mail->WordWrap = 50;                                 // set word wrap to 50 characters
	//$mail->AddAttachment("/tmp/image.jpg", "new.jpg");    // optional name
	$mail->IsHTML(true);                                  // set email format to HTML

	$mail->Subject = $subject;
	$mail->Body    = $htmlbody;
	$mail->AltBody = $plainbody;
	
	if($mail->Send())
	{
		return true;
	}
	else
	{
		return false;
	}
}

function whois_query($domain) {

    // fix the domain name:
    $domain = strtolower(trim($domain));
    $domain = preg_replace('/^http:\/\//i', '', $domain);
    $domain = preg_replace('/^www\./i', '', $domain);
    $domain = explode('/', $domain);
    $domain = trim($domain[0]);

    // split the TLD from domain name
    $_domain = explode('.', $domain);
    $lst = count($_domain) - 1;
    $ext = $_domain[$lst];

    // You find resources and lists 
    // like these on wikipedia: 
    //
    // http://de.wikipedia.org/wiki/Whois
    //
    $servers = array(
        "biz" => "whois.neulevel.biz",
        "com" => "whois.internic.net",
        "us" => "whois.nic.us",
        "coop" => "whois.nic.coop",
        "info" => "whois.nic.info",
        "name" => "whois.nic.name",
        "net" => "whois.internic.net",
        "gov" => "whois.nic.gov",
        "edu" => "whois.internic.net",
        "mil" => "rs.internic.net",
        "int" => "whois.iana.org",
        "ac" => "whois.nic.ac",
        "ae" => "whois.uaenic.ae",
        "at" => "whois.ripe.net",
        "au" => "whois.aunic.net",
        "be" => "whois.dns.be",
        "bg" => "whois.ripe.net",
        "br" => "whois.registro.br",
        "bz" => "whois.belizenic.bz",
        "ca" => "whois.cira.ca",
        "cc" => "whois.nic.cc",
        "ch" => "whois.nic.ch",
        "cl" => "whois.nic.cl",
        "cn" => "whois.cnnic.net.cn",
        "cz" => "whois.nic.cz",
        "de" => "whois.nic.de",
        "fr" => "whois.nic.fr",
        "hu" => "whois.nic.hu",
        "ie" => "whois.domainregistry.ie",
        "il" => "whois.isoc.org.il",
        "in" => "whois.ncst.ernet.in",
        "ir" => "whois.nic.ir",
        "mc" => "whois.ripe.net",
        "to" => "whois.tonic.to",
        "tv" => "whois.tv",
        "ru" => "whois.ripn.net",
        "org" => "whois.pir.org",
        "aero" => "whois.information.aero",
        "nl" => "whois.domain-registry.nl"
    );

    if (!isset($servers[$ext])) {
        die('Error: No matching nic server found!');
    }

    $nic_server = $servers[$ext];

    $output = '';

    // connect to whois server:
    if ($conn = fsockopen($nic_server, 43)) {
        fputs($conn, $domain . "\r\n");
        while (!feof($conn)) {
            $output .= fgets($conn, 128);
        }
        fclose($conn);
    } else {
        die('Error: Could not connect to ' . $nic_server . '!');
    }

    return $output;
}

function detect_city($ip) {

    $default = 'UNKNOWN';

    if (!is_string($ip) || strlen($ip) < 1 || $ip == '127.0.0.1' || $ip == 'localhost')
        $ip = '8.8.8.8';

    $curlopt_useragent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)';

    $url = 'http://ipinfodb.com/ip_locator.php?ip=' . urlencode($ip);
    $ch = curl_init();

    $curl_opt = array(
        CURLOPT_FOLLOWLOCATION => 1,
        CURLOPT_HEADER => 0,
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_USERAGENT => $curlopt_useragent,
        CURLOPT_URL => $url,
        CURLOPT_TIMEOUT => 1,
        CURLOPT_REFERER => 'http://' . $_SERVER['HTTP_HOST'],
    );

    curl_setopt_array($ch, $curl_opt);

    $content = curl_exec($ch);

    if (!is_null($curl_info)) {
        $curl_info = curl_getinfo($ch);
    }

    curl_close($ch);

    if (preg_match('{<li>City : ([^<]*)</li>}i', $content, $regs)) {
        $city = $regs[1];
    }
    if (preg_match('{<li>State/Province : ([^<]*)</li>}i', $content, $regs)) {
        $state = $regs[1];
    }

    if ($city != '' && $state != '') {
        $location = $city . ', ' . $state;
        return $location;
    } else {
        return $default;
    }
}

 //
 //Validate Email address
 function domain_exists($email,$record = 'MX')
{
	list($user,$domain) = split('@',$email);
	return checkdnsrr($domain,$record);
}
?>